package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.requestbean.BaseResponseBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.slidemenu.SwipeMenu;
import com.mmlc.meimei.lichengkongjian.customview.slidemenu.SwipeMenuCreator;
import com.mmlc.meimei.lichengkongjian.customview.slidemenu.SwipeMenuItem;
import com.mmlc.meimei.lichengkongjian.customview.slidemenu.SwipeMenuListView;
import com.mmlc.network.filter.AddressListRequestFilter;
import com.mmlc.network.filter.ClearAddressRequestFilter;
import com.mmlc.network.responsebean.AddressListResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;


public class InformationSelectActivity extends InterActivity  {

    @Override
    protected int initContentView() {
        return R.layout.activity_information_select;
    }
    SwipeMenuListView lv_shopList;
    List<AddressListResponseBean.DataBean> datas;
    AddressAdapter adapter;
    ImageView iv_add;
    String from;

    protected void initView() {
        from = getIntent().getStringExtra("from");
        iv_add = ViewUtils.getView(activity, R.id.iv_add);
        lv_shopList = (SwipeMenuListView) findViewById(R.id.swipelist);
        iv_add.setOnClickListener(this);
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        datas = new ArrayList<>();

    }


    @Override
    protected void initInfo() {
        AddressListRequestFilter filter = new AddressListRequestFilter( activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<AddressListResponseBean>() {
            @Override
            public void onSuccess(AddressListResponseBean result) {
                displayAddressList(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
            }
        });
    }

    int selectedAddress = -1;

    private void displayAddressList(AddressListResponseBean result) {
        datas.clear();
        datas.addAll(result.getData());
        for (int i = 0; i < datas.size(); i++) {
            if (!ViewUtils.isEmpty(datas.get(i).getIs_default()) && "1".equals(datas.get(i).getIs_default())) {
                selectedAddress = i;
                break;
            }
        }
        adapter = new AddressAdapter(InformationSelectActivity.this,datas);
        lv_shopList.setAdapter(adapter);
        intiMenu();

    }

    public void intiMenu(){
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem editItem = new SwipeMenuItem(
                        context);
                editItem.setBackground(new ColorDrawable(Color.rgb(0, 0, 0)));
                WindowManager windowManager = getWindowManager();
                int width = windowManager.getDefaultDisplay().getWidth();

                editItem.setWidth((int)(width*0.2));
                editItem.setTitle("编辑");
                editItem.setTitleSize(17);
                editItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(editItem);

                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        context);
                deleteItem.setBackground(new ColorDrawable(Color.rgb(255, 0, 0)));
                deleteItem.setWidth((int)(width*0.2));
                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(17);
                deleteItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        lv_shopList.setMenuCreator(creator);

        lv_shopList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        Intent intent = new Intent(InformationSelectActivity.this, EditInformationActivity.class);
                        intent.putExtra("id", datas.get(position).getId());
                        intent.putExtra("type", 1);
                        /** type 0:添加；1：修改 */
                        intent.putExtra("address", datas.get(position));
                        intent.putExtra("position", position);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        break;
                    case 1:
                        deleteAddress(datas.get(position),position);
                        break;
                }
            }
        });
        lv_shopList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(!TextUtils.isEmpty(from) && from.equals("order")){
                    Intent intent = new Intent();
                    intent.putExtra("id",datas.get(position).getId());
                    intent.putExtra("name",datas.get(position).getContactor());
                    intent.putExtra("phone",datas.get(position).getPhone());
                    StringBuffer sb = new StringBuffer();
                    if (!ViewUtils.isEmpty(datas.get(position).getProvince_name()))
                        sb.append(datas.get(position).getProvince_name() + "/");
                    if (!ViewUtils.isEmpty(datas.get(position).getCity_name()))
                        sb.append(datas.get(position).getCity_name() + "/");
                    if (!ViewUtils.isEmpty(datas.get(position).getArea_name()))
                        sb.append(datas.get(position).getArea_name() + "/");
                    if (!ViewUtils.isEmpty(datas.get(position).getAddress()))
                        sb.append(datas.get(position).getAddress());
                    intent.putExtra("address",sb.toString());
                    setResult(200,intent);
                    finish();
                }

            }
        });

    }



    private void deleteAddress(final AddressListResponseBean.DataBean dataBean, final int position) {
        ClearAddressRequestFilter filter = new ClearAddressRequestFilter( activity);
        filter.requestBean.aid = dataBean.getId();
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BaseResponseBean>() {
            @Override
            public void onSuccess(BaseResponseBean result) {
                ToastUtils.showToast("删除成功");
                datas.remove(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
            }
        });
    }






    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_add:
                Intent intent = new Intent(activity, EditInformationActivity.class);
                /** type 0:添加；1：修改 */
                intent.putExtra("type", 0);
                startActivityForResult(intent, 201);
                break;
        }
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initInfo();
    }


}
